﻿using System;
using System.Diagnostics;

using SharedGenomics.Annotations;

namespace SharedGenomics.Workbench.Annotations
{
    public class LookupAnnotationRequestStage : IAnnotationRequestStage
    {
        private readonly IAnnotationRepositoryMediator _annotationPool;

        public LookupAnnotationRequestStage(IAnnotationRepositoryMediator annotationPool)
        {
            this._annotationPool = annotationPool;
        }

        public void Process(AnnotationRequestContext context, IAnnotationRequestPipelineTraversalStrategy traversalStrategy)
        {
            //see if the requested annotation is alredy in the pool
            object annotation = this._annotationPool.GetAnnotationForTarget(context.RequestKey, context.AnnotationTarget);

            if (annotation != null)
            {
                IAnnotation a = annotation as IAnnotation;
                Debug.Assert(a != null);

                context.AddAnnotation(a, null);
            }

            traversalStrategy.MoveNext(context);
        }
    }
}
